package com.tanhua.server.controller;

import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.QuantityVo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.MovementService;
import com.tanhua.server.service.TanhuaService;
import com.tanhua.server.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private MovementService movementService;
    @Autowired
    private TanhuaService tanhuaService;


    /**
     * 查询用户资料
     *    参数：token
     */
    @GetMapping
    public ResponseEntity users(@RequestHeader("Authorization") String token,Long userID) {
        if(userID == null) {
            userID = UserHolder.getUserId();
        }
        UserInfoVo userInfo = userInfoService.findById(userID);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 更新用户资料
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo 	userInfo,@RequestHeader("Authorization") String token) {
        userInfo.setId(UserHolder.getUserId());
        System.out.println("============================="+userInfo);
        userInfoService.update(userInfo);
        return ResponseEntity.ok("更新成功"+userInfo);
    }

    /**
     * 喜欢(粉丝)
     */
    @PostMapping("/fans/{uid}")
    public ResponseEntity focus(@PathVariable("uid") Long likeUserId) {
        tanhuaService.likeUser(likeUserId);
        return ResponseEntity.ok(null);
    }

    /**
     * 取消喜欢
     */
    @DeleteMapping("/like/{uid}")
    public ResponseEntity dislike(@PathVariable("uid") Long likeUserId) {
        tanhuaService.notLikeUser(likeUserId);
        return ResponseEntity.ok(null);
    }

    /**
     * 互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
     */
    @GetMapping("/friends/{type}")
    public ResponseEntity friendsList(@PathVariable("type") Integer type,
                                      @RequestParam(defaultValue = "1") Integer page,
                                      @RequestParam(defaultValue = "10") Integer pagesize,
                                      String nickname) {
        if (type == 1) {
            PageResult pr = tanhuaService.likeEachOther(page, pagesize, nickname);
            return ResponseEntity.ok(pr);
        } else if (type == 2) {
            PageResult pr = tanhuaService.iLike(page, pagesize, nickname);
            return ResponseEntity.ok(pr);
        } else if (type == 3) {
            PageResult pr = tanhuaService.likeMe(page, pagesize, nickname);
            return ResponseEntity.ok(pr);
        } else {
            PageResult pr = tanhuaService.visitors(page, pagesize, nickname);
            return ResponseEntity.ok(pr);
        }
    }

    /**
     * 互相喜欢，喜欢，粉丝 - 统计
     */
    @GetMapping("/counts")
    public ResponseEntity counts() {
        QuantityVo vo = tanhuaService.counts();
        return ResponseEntity.ok(vo);
    }


    /**
     * 用户资料 - 更新头像
     * @param headPhoto
     * @return
     */
    @PostMapping("/header")
    public ResponseEntity updateHeader(MultipartFile headPhoto) throws IOException {
        Long userId = UserHolder.getUserId();
        userInfoService.updateHead(headPhoto,userId);
        return ResponseEntity.ok(null);
    }


    //是否喜欢（新增接口）
    @GetMapping("/{id}/alreadyLove")
    public ResponseEntity alreadyLove(@PathVariable("id") Long uid){
        Boolean loveOrNot = tanhuaService.loveOrNot(uid);
        return ResponseEntity.ok(loveOrNot);
    }

}